#include "stdafx.h"
#include "QtWidgetsApplication.h"
#include <windows.h>
#include <commctrl.h>
#include <iostream>   // 包含Windows API头文件
QtWidgetsApplication::QtWidgetsApplication(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.pushButton, &QPushButton::clicked, this, &QtWidgetsApplication::test);

    // 将"计算机"图标移动到位置(100, 50)
    
}

void QtWidgetsApplication::test() 
{
    bool success = moveDesktopIcon("计算机", 100, 50);

    // 将"回收站"图标移动到位置(200, 100)  
    bool success2 = moveDesktopIcon("回收站", 200, 100);
  //  QMessageBox::information(NULL,"提示信息","点击事件");
}





// 移动桌面图标到指定位置
bool QtWidgetsApplication::moveDesktopIcon(const QString& iconName, int newX, int newY)
{
    // 获取桌面ListView
  //  HWND hDesktop = FindWindow(L"WorkerW", NULL);
    HWND hDesktop = FindWindow(L"Progman", NULL);
    HWND hShellDefView = FindWindowEx(hDesktop, 0, L"SHELLDLL_DefView", NULL);
    HWND hListView = FindWindowEx(hShellDefView, 0, L"SysListView32", L"FolderView");
    int iconCount2 = ListView_GetItemCount(hListView);
  


    int iconCount = ListView_GetItemCount(hListView);



    if (!hListView) return false;

    // 查找并移动图标
    int count = ListView_GetItemCount(hListView);
    for (int i = 0; i < count; i++) {
        wchar_t name[256];
        LVITEM item = { LVIF_TEXT, i, 0, 0, 0, name, 256 };
       // if (ListView_GetItem(hListView, &item)) {}
        QString value=  QString::fromWCharArray(name);
            if (QString::fromWCharArray(name) == iconName) {
                POINT pos = { newX, newY };
                ListView_SetItemPosition(hListView,i,  pos.x, pos.y);
                return true;
            }
       
    }
    return false;
}



HWND QtWidgetsApplication::GetDesktopListView()
{
    // 查找桌面窗口
    HWND hDesktop = FindWindow(L"Progman", L"Program Manager");
    if (!hDesktop) return NULL;

    // 查找ShellDLL_DefView窗口
    HWND hShellView = FindWindowEx(hDesktop, NULL, L"SHELLDLL_DefView", NULL);
    if (!hShellView) {
        // 有时需要先向Progman发送消息来初始化桌面
        SendMessage(hDesktop, 0x052C, 0, 0);
        hShellView = FindWindowEx(hDesktop, NULL, L"SHELLDLL_DefView", NULL);
    }
    if (!hShellView) return NULL;

    // 查找SysListView32控件（实际的图标列表）
    return FindWindowEx(hShellView, NULL, L"SysListView32", NULL);
}

QtWidgetsApplication::~QtWidgetsApplication()
{}

